var useFiles = [];

useFiles.push('page.css');
useFiles.push('easyui-icon.css');
useFiles.push('easyloader');

seajs.use(useFiles, function() {
			easyloader.theme = fand.theme;

			easyloader.locale = fand.locale;

			$(initPage);
		});

function initPage() {
	$(document).on("click", 'a[href="#ok"]', ok_click);

	$(document).on("click", 'a[href="#reset"]', reset_click);

	$(document).on("click", 'a[href="#back"]', back_click);
}

function ok_click() {
	queryParams['site.code'] = $('input[name="site.code"]').val();
	queryParams['site.domainNaCo'] = $('input[name="site.domainNaCo"]').val();

	$('.easyui-datagrid').datagrid('reload');

	back_click();
}

function reset_click() {
	$('form').each(function() {
				this.reset();
			});
}

function back_click() {
	using('window', function() {
				$windowForSearch = $('#windowForSearch');

				$windowForSearch.window('close');
			});
}

function onDblClickRow(rowIndex, rowData) {
	var src = fand.webContextUrl + '/admin/site/site/view.htm?id=' + rowData.ID;

	using('window', function() {
				$windowForIframe = $('#windowForIframe');

				$windowForIframe.find('iframe').prop('src', src);

				$windowForIframe.window({
							title : '查看',
							width : ($('html').width() * 0.8),
							height : ($('html').height() * 0.8),
							modal : true
						});
			});
}

var queryParams = {};

function onBeforeLoad(param) {
	param['site.paginationNo'] = param.page;
	param['site.paginationSize'] = param.rows;

	$.extend(param, queryParams);
}

function add_handler() {
	var src = fand.webContextUrl + '/admin/site/site/new.htm';

	using('window', function() {
				$windowForIframe = $('#windowForIframe');

				$windowForIframe.find('iframe').prop('src', src);

				$windowForIframe.window({
							title : '新增',
							width : ($('html').width() * 0.8),
							height : ($('html').height() * 0.8),
							modal : true
						});
			});

	return false;
}

function edit_handler() {
	var row = $('.easyui-datagrid').datagrid('getSelected');

	if (row == null) {
		using(['messager', 'tooltip'], function() {
					$.messager.alert('提示消息', '请选择需编辑的数据！', 'warning');
				});

		return false;
	}

	var src = fand.webContextUrl + '/admin/site/site/edit.htm?id=' + row.ID;

	using('window', function() {
				$windowForIframe = $('#windowForIframe');

				$windowForIframe.find('iframe').prop('src', src);

				$windowForIframe.window({
							title : '编辑',
							width : ($('html').width() * 0.8),
							height : ($('html').height() * 0.8),
							modal : true
						});
			});

	return false;
}

function remove_handler() {
	var rows = $('.easyui-datagrid').datagrid('getChecked');

	if (rows.length < 1) {
		using(['messager', 'tooltip'], function() {
					$.messager.alert('提示消息', '请勾选需删除的数据！', 'warning');
				});

		return false;
	}

	using(['messager', 'tooltip'], function() {
				$.messager.confirm('确认', '将删除勾选的数据?', function(r) {
							if (r) {
								var ids = [];

								$(rows).each(function(i, row) {
											ids.push(row.ID);
										});

								var url = fand.webContextUrl + '/admin/site/site/delete.json';

								$.messager.progress();

								$.post(url, {
											ids : ids.join(',')
										}, function(data) {
											$.messager.progress('close');

											if (data.message == undefined) {
												$.messager.alert('提示消息', '删除成功！', 'info');

												$('.easyui-datagrid').datagrid('reload');

												return;
											}

											$.messager.alert('提示消息', '删除失败:' + data.message, 'error');
										});
							}
						});
			});

	return false;
}

function view_handler() {
	var row = $('.easyui-datagrid').datagrid('getSelected');

	if (row == null) {
		using(['messager', 'tooltip'], function() {
					$.messager.alert('提示消息', '请选择需查看的数据！', 'warning');
				});

		return false;
	}

	onDblClickRow((row.RN - 1), row);
}

function search_handler() {
	using('window', function() {
				$windowForSearch = $('#windowForSearch');

				$windowForSearch.window({
							width : ($('html').width() * 0.5),
							height : ($('html').height() * 0.5),
							modal : true
						});
			});

	return false;
}

var toolbar = [{
			text : '新增',
			iconCls : 'icon-add',
			handler : add_handler
		}, {
			text : '编辑',
			iconCls : 'icon-edit',
			handler : edit_handler
		}, '-', {
			text : '批量删除',
			iconCls : 'icon-remove',
			handler : remove_handler
		}, '-', {
			text : '查看',
			iconCls : 'icon-tip',
			handler : view_handler
		}, '-', {
			text : '查询',
			iconCls : 'icon-search',
			handler : search_handler
		}];